package com.launch.carmanager.lanuncher;

import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.launch.carmanager.R;
import com.launch.carmanager.common.base.BaseActivity;
import com.launch.carmanager.common.utils.DrawableUtils;
import com.launch.carmanager.common.widget.CMViewPager;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.badgeview.BGABadgeLinearLayout;

/**
 * Created by ganquan on 2019/3/29 11:37
 */
public class LauncherActivity extends BaseActivity {

    @BindView(R.id.view_pager)
    CMViewPager viewPager;
    @BindView(R.id.tab_layout)
    SmartTabLayout tabLayout;

    private LauncherAdapter mAdapter;
    private BGABadgeLinearLayout mBadgeView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher_layout);
        mAdapter = new LauncherAdapter(getSupportFragmentManager(), getContext());
        viewPager.setAdapter(mAdapter);

        tabLayout.setCustomTabView(mTabProvider);
        tabLayout.setViewPager(viewPager);

        requestStoragePermission();
    }

    private SmartTabLayout.TabProvider mTabProvider = new SmartTabLayout.TabProvider() {
        @Override
        public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
            View tab = inflateTabView(container);

            ImageView icon = tab.findViewById(R.id.custom_tab_icon);
            icon.setImageDrawable(getTintedTabIcon(position));

            TextView text = tab.findViewById(R.id.custom_tab_text);
            text.setText(mAdapter.getPageTitle(position));
            text.setTextColor(colorTab);

            if (position == 0) {
                mBadgeView = tab.findViewById(R.id.badge_view);
            }

            return tab;
        }
    };

    private Drawable getTintedTabIcon(int position) {
        int iconId = TAB_ICONS[position % TAB_ICONS.length];
        return DrawableUtils.setTintList(getContext(), iconId, null);


    }


    private final int[] TAB_ICONS = new int[]{
            R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
    };

    private View inflateTabView(ViewGroup container) {
        return getLayoutInflater().inflate(R.layout.custom_tab_view, container, false);
    }

    @BindColor(R.color.selector_launcher_tab)
    ColorStateList colorTab;


}
